package com.snowwolf.trade.controller;

import com.snowwolf.trade.base.RestResponse;
import com.snowwolf.trade.entity.TradeEo;
import com.snowwolf.trade.service.ITradeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: snowwolf
 * @description:
 * @date: Create in 2019/2/25 9:55
 * @modified by:
 * @versions：0.1.0
 */
@RestController
@RequestMapping("/v1/trade")
public class TradeController {
    private static Logger logger = LoggerFactory.getLogger(TradeController.class);
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private ITradeService tradeService;
    @GetMapping("/get")
    public RestResponse getTrade(Long id){
        List<TradeEo> list = new ArrayList<>();
        for(int i=0;i<10;i++){
            list.add(tradeService.findById(id).getData());
        }
        List<TradeEo> collect = list.stream().distinct().collect(Collectors.toList());
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("trade");
        logger.info("serviceId is ={}, host is = {}, port is ={}",
                serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
        return tradeService.findById(id);
    }
}
